package com.heu.blood.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.security.dao.UserRoleDao;
import com.heu.blood.security.entity.UserRoleEntity;
import com.heu.blood.security.service.UserRoleService;


@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRoleEntity> implements UserRoleService {

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserRoleEntity> page = this.page(
                new Query<UserRoleEntity>().getPage(params),
                new QueryWrapper<UserRoleEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过userId获取对应的roleId
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRoleByUser(Long userId) {
        // 构造queryWrapper对象
        LambdaQueryWrapper<UserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(userId.toString()), UserRoleEntity::getUserId, userId);
        // 返回结果
        List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(queryWrapper);
        List<Long> roleIds = userRoleEntities.stream().map(userRoleEntity -> userRoleEntity.getRoleId()).collect(Collectors.toList());
        return roleIds;
    }

}